package geez.widget;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

import static geez.widget.Geez.withNameAndId;

public class GFlexTable extends FlexTable implements GWidget, GTable {

    int currentRow = 0;

    public GFlexTable id(String id) {
        withNameAndId(this, id);
        return this;
    }

    public GFlexTable css(String cssClass, String... optionalClasses) {
        Geez.withStyle(this, cssClass, optionalClasses);
        return this;
    }

    public GFlexTable show() {
        setVisible(true);
        return this;
    }

    public GFlexTable hide() {
        setVisible(false);
        return this;
    }

    public GFlexTable set(int row, int column, Widget widget) {
        setWidget(row, column, widget);
        return this;
    }

    public GRow addRow(Widget... widgets) {
        int row = currentRow++;
        int column = 0;
        for (Widget widget : widgets) {
            set(row, column++, widget);
        }
        return new GRow(this, row);
    }

    public GFlexTable fillInRowsUpTo(int row, String cellCss) {
        int numberOfRows = getRowCount();
        for (int rowIndex = numberOfRows; rowIndex < row; rowIndex++) {
            int cells = getCellCount(0);
            for (int columnIndex = 0; columnIndex < cells; columnIndex++) {
                set(rowIndex, columnIndex, Geez.clearOnly());
                getCellFormatter().setStylePrimaryName(rowIndex, columnIndex, cellCss);
            }
        }
        return this;
    }

    public GFlexTable cellspacing(int cellspacing) {
        setCellSpacing(cellspacing);
        return this;
    }

    public GFlexTable cellpadding(int cellpadding) {
        setCellPadding(cellpadding);
        return this;
    }

    public GFlexTable columnFormatter(ColumnFormatter formatter) {
        setColumnFormatter(formatter);
        return this;
    }

    public GFlexTable cellFormatter(CellFormatter formatter) {
        setCellFormatter(formatter);
        return this;
    }

    public class GRow {
        private GFlexTable table;
        private int row;

        public GRow(GFlexTable table, int row) {
            this.table = table;
            this.row = row;
        }

        public GFlexTable table() {
            return table;
        }

        public GRow css(String css) {
            table.getRowFormatter().setStyleName(row, css);
            return this;
        }

        public GRow addCss(String css) {
            table.getRowFormatter().addStyleName(row, css);
            return this;
        }

        public GRow cellCss(String... styles) {
            for (int i = 0; i < styles.length; i++) {
                String style = styles[i];
                table.getCellFormatter().setStylePrimaryName(row, i, style);
            }
            return this;
        }

        public GRow id(String id) {
            table.getRowFormatter().getElement(row).setId(id);
            return this;
        }

        public int getRowIndex() {
            return row;
        }
    }

}
